<template>
  <div>
    <el-card style="margin:20px 0">
      <!-- 引入三级分类组件 -->
      <Catergory :show="status !== 0 " @getCategoryList="getCategoryListId" />
    </el-card>

    <el-card>
      <div v-show="status === 0">
        <!-- 展示spu数据列表 -->
        <!-- 按钮 -->
        <el-button type="primary" icon="el-icon-plus" :disabled="!Catergory3Id" @click="addSpu()">添加SPU</el-button>
        <!-- 表格 -->
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center" />
          <el-table-column prop="spuName" label="SPU名称" width="width" />
          <el-table-column prop="description" label="SPU描述" width="width" />
          <el-table-column label="操作" width="width">
            <template slot-scope="{row}">
              <hint-button type="success" size="mini" icon="el-icon-plus" title="添加sku" @click="showSkuForm(row)" />
              <hint-button type="warning" size="mini" icon="el-icon-edit" title="修改spu" @click="editSpu(row)" />
              <hint-button type="info" size="mini" icon="el-icon-info" title="查看当前spu的sku的列表" @click="lookSku(row)" />
              <el-popconfirm title="确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button slot="reference" type="danger" size="mini" icon="el-icon-delete" title="删除spu" />
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          style="text-align:center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper, ->,sizes,total"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="getSpuList"
        />
      </div>
      <SpuForm v-show="status === 1" ref="spuForm" @updateStatus="updateStatus" />
      <SkuForm v-show="status === 2" ref="skuForm" @showStatus="showStatus" />
      <!-- 显示sku列表 -->
      <el-dialog :title="`${spuName}的sku列表`" :visible="dialogTableVisible" @close="close">
        <el-table
          v-loading="loading"
          style="width: 100%"
          border
          :data="skuList"
        >
          <el-table-column
            prop="skuName"
            label="名称"
            width="width"
          />
          <el-table-column
            prop="price"
            label="价格"
            width="width"
          />
          <el-table-column
            prop="weight"
            label="重量"
            width="width"
          />
          <el-table-column
            prop="prop"
            label="默认图片"
            width="width"
          >
            <template slot-scope="{row}">
              <img :src="row.skuDefaultImg" style="width:100px" alt="">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import SkuForm from './SkuForm'
import SpuForm from './SpuForm'
export default {
  name: 'SPU',
  components: {
    SkuForm,
    SpuForm
  },
  data() {
    return {
      loading: true,
      spuName: '',
      dialogTableVisible: false,
      Catergory1Id: '',
      Catergory2Id: '',
      Catergory3Id: '',
      page: 1,
      limit: 3,
      records: [],
      skuList: [],
      total: 0,
      status: 0 // 0 代表spu列表 1 代表添加修改spu 2 代表添加sku
    }
  },
  methods: {
    // 弹窗关闭
    close() {
      this.dialogTableVisible = false
      this.skuList = []
      this.loading = true
    },
    // 查看sku列表
    async lookSku(row) {
      this.dialogTableVisible = true
      this.spuName = row.spuName
      const res = await this.$API.spu.reqSkuList(row.id)
      this.skuList = res.data
      this.loading = false
    },
    // 自定义事件(skuform)
    showStatus(status) {
      this.status = status
    },
    // 显示sku表单
    showSkuForm(row) {
      this.status = 2
      this.$refs.skuForm.getData(this.Catergory1Id, this.Catergory2Id, row)
    },
    // 编辑spu
    editSpu(row) {
      this.status = 1
      this.$refs.spuForm.initSpuData(row)
    },
    //  自定义事件（spuform）修改展示数据
    updateStatus(data) {
      // 0 没有ID
      this.status = data.status
      if (data.leavl === 0) {
        this.getSpuList()
      }
      if (data.leavl === 1) {
        this.getSpuList(this.page)
      }
    },
    //  获取分类的ID
    getCategoryListId({ Catergory2Id, Catergory3Id, Catergory1Id, leave }) {
      if (leave === 1) {
        this.Catergory1Id = Catergory1Id
        this.Catergory2Id = ''
        this.Catergory3Id = ''
      } else if (leave === 2) {
        this.Catergory2Id = Catergory2Id
        this.Catergory3Id = ''
      } else {
        this.Catergory3Id = Catergory3Id
        this.getSpuList()
      }
    },
    // 获取spu列表数据
    async getSpuList(pages = 1) {
      this.page = pages
      const { page, limit, Catergory3Id } = this
      const res = await this.$API.spu.getSpuList(page, limit, Catergory3Id)
      if (res.code === 200) {
        this.records = res.data.records
        this.total = res.data.total
      } else {
        this.$message.error(res.message)
      }
    },
    // 每页数据条数的变化
    handleSizeChange(num) {
      this.limit = num
      this.getSpuList()
    },
    // 添加spu
    addSpu() {
      this.status = 1
      this.$refs.spuForm.initSpuData(this.Catergory3Id)
    },
    // 删除spu
    async deleteSpu(row) {
      // reqDeleteSpu
      try {
        await this.$API.spu.reqDeleteSpu(row.id)
        this.$message.success('删除成功')
        this.getSpuList(this.records.length <= 1 ? this.page - 1 : this.page)
      } catch (error) {
        return
      }
    }
  }
}
</script>

<style>

</style>
